﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace huqiang.FileTool
{
    public class ShareFile
    {
        static long FID = 100;
        static List<RealFile> files = new List<RealFile>();
        static List<ShareFile> shares = new List<ShareFile>();
        static string dic = Environment.CurrentDirectory.Replace('\\', '/') + '/';
        public static void ForceDisposeAll()
        {
            for (int i = 0; i < files.Count; i++)
                files[i].stream.Dispose();
            files.Clear();
            for (int i = 0; i < shares.Count; i++)
                shares[i].Context = null;
            shares.Clear();
        }
        public class RealFile
        {
            public string path;
            public Stream stream;
            public int refCount;
            public long fileID;
        }
        RealFile Context;
        private ShareFile()
        { 
        }
        public static long QueryFileID(string path)
        {
            path = path.Replace('\\', '/');
            path = path.Replace(dic, "");
            for(int i=0;i<files.Count;i++)
            {
                if (files[i].path == path)
                    return files[i].fileID;
            }
            return 0;
        }
        public static ShareFile Open(string path)
        {
            if (!File.Exists(path))
                return null;
            path = path.Replace('\\', '/');
            path = path.Replace(dic, "");
            ShareFile file=new ShareFile();
            shares.Add(file);
            for (int i = 0; i < files.Count; i++)
            {
                var real = files[i];
                if(real.path==path)
                {
                    file.Context = real;
                    real.refCount++;
                    return file;
                }
            }
            FID++;
            RealFile rf = new RealFile();
            rf.refCount = 1;
            rf.path = path;
            rf.fileID = FID;
            rf.stream = File.Open(path,FileMode.Open);
            files.Add(rf);
            file.Context = rf;
            return file;
        }
        public long FileID { get {
                if (Context != null)
                    return Context.fileID;
                return 0;
            } }
        /// <summary>
        /// 请勿使用Stream的相关释放函数
        /// </summary>
        public Stream ShareStream
        {
            get
            {
                if (Context != null)
                    return Context.stream;
                return null;
            }
        }
        public void Dispose()
        {
            if (Context != null)
            {
                Context.refCount--;
                if (Context.refCount <= 0)
                {
                    Context.stream.Dispose();
                    files.Remove(Context);
                }
                Context = null;
            }
            shares.Remove(this);
        }
    }
}
